/**
  ******************************************************************************
  * @file    bsp_gprs.c
  * @author  Iron
  * @date    2018-08-07
  * @version v1.0
  * @brief   hal gprs c file
  */

/** @addtogroup GROUP_BSP
  * @{
  */

/* includes ------------------------------------------------------------------*/
#include <ctype.h>
#include <string.h>
#include "stm32f4xx_hal.h"
#include "FreeRTOS.h"
#include "task.h"
#include "bsp_gprs.h"
#include "bsp_power.h"

/* private define ------------------------------------------------------------*/
#ifndef EOF
    #define EOF (-1)
#endif

// gpio config
#define GPRS_PWRKEY_PIN     GPIO_PIN_0       // GPRS_PWRKEY  PB0  - 35
#define GPRS_PWRKEY_PORT    GPIOB
#define GPRS_PWRKEY_LOW()   HAL_GPIO_WritePin(GPRS_PWRKEY_PORT, GPRS_PWRKEY_PIN, GPIO_PIN_SET)
#define GPRS_PWRKEY_HIGH()  HAL_GPIO_WritePin(GPRS_PWRKEY_PORT, GPRS_PWRKEY_PIN, GPIO_PIN_RESET)

#define GPRS_NETLIGHT_PIN   GPIO_PIN_14      // NET_LIGHT    PE14 - 45
#define GPRS_STATUS_PIN     GPIO_PIN_15      // GSM_STA      PE15 - 46
#define GPRS_NETLIGHT_STATUS_PORT    GPIOE

#define GPRS_GET_STATUS()   HAL_GPIO_ReadPin(GPRS_NETLIGHT_STATUS_PORT, GPRS_STATUS_PIN)

#define GPRS_PORT_RCC_INIT()  \
          __HAL_RCC_GPIOB_CLK_ENABLE(); \
          __HAL_RCC_GPIOC_CLK_ENABLE(); \
          __HAL_RCC_GPIOE_CLK_ENABLE()

// UART5_RX PD2
// UART5_TX PC12

#define GPRS_USART  BSP_UART5

/* private typedef -----------------------------------------------------------*/
/* private macro -------------------------------------------------------------*/
/* private variables ---------------------------------------------------------*/
static bsp_usart_handle_t gprs_usart;

/* private function prototypes -----------------------------------------------*/
/* private functions ---------------------------------------------------------*/

/**
  * @brief  init gprs module io port
  * @param  None.
  * @retval None.
  */
void gprs_gpio_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    GPRS_PORT_RCC_INIT();

    // GPRS PWRKEY
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPRS_PWRKEY_HIGH();
    GPIO_InitStruct.Pin = GPRS_PWRKEY_PIN;
    HAL_GPIO_Init(GPRS_PWRKEY_PORT, &GPIO_InitStruct);

    // NETLIGHT / STATUS
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Pin = GPRS_NETLIGHT_PIN | GPRS_STATUS_PIN;
    HAL_GPIO_Init(GPRS_NETLIGHT_STATUS_PORT, &GPIO_InitStruct);
}

/**
  * @brief  gprs power on
  * @param  None.
  * @retval gprs power pin state.
  */
void gprs_power_on(void)
{
    bsp_power_set(bsp_power_gprs, BSP_POWER_ON);
}

/**
  * @brief  gprs power off
  * @param  None.
  * @retval gprs power pin state.
  */
void gprs_power_off(void)
{
    bsp_power_set(bsp_power_gprs, BSP_POWER_OFF);
}

/**
  * @brief  get gprs status.
  * @param  None.
  * @retval gprs status
  *         - 0: shutdown
  *         - 1: running
  */
uint32_t gprs_get_power_status(void)
{
    return GPRS_GET_STATUS();
}

/**
  * @brief  gprs PWRKEY pulse for gprs module on/off
  * @param  None.
  * @retval None.
  */
void gprs_pwrkey_pulse(void)
{
    GPRS_PWRKEY_LOW();
    vTaskDelay(pdMS_TO_TICKS(1000));
    GPRS_PWRKEY_HIGH();
}

/**
  * @brief  gprs uart api
  *
  */
int32_t gprs_uart_init(usart_ex_isr_rx_indicate_t rx_indicate)
{
    return bsp_usart_ex_init(&gprs_usart, GPRS_USART, 115200, rx_indicate, NULL);
}

int32_t gprs_uart_send(uint8_t *data, int32_t len, uint32_t timeout)
{
    return bsp_usart_send_ex(&gprs_usart, data, len, timeout);
}

int32_t gprs_uart_getbyte(uint8_t *data, uint32_t timeout)
{
    return bsp_usart_getbyte_ex(&gprs_usart, data, timeout);
}

int32_t gprs_uart_recv(uint8_t *data, int32_t size, uint32_t timeout)
{
    return bsp_usart_recv_ex(&gprs_usart, data, size, timeout);
}

/**
  * @}
  */

/******************* (C)COPYRIGHT 2018 *****END OF FILE***********************/
